/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Complaint;
import entity.Members;
import entity.Solution;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import managerBeanViews.Login;
import processor.ComplaintFacade;
import processor.MembersFacade;
import processor.SolutionFacade;

/**
 *
 * @author maynoigi
 */
@ManagedBean
@SessionScoped
public class ComplaintsBean implements Serializable {

    @EJB
    private MembersFacade membersFacade;
    @EJB
    private SolutionFacade solutionFacade;
    @EJB
    private ComplaintFacade complaintFacade;
    @ManagedProperty(value = "#{login}")//this is EL name of your bean 
    private Login injectedBean;
    List<Complaint> complaints;
    List<Complaint> complaintsWaits;
    List<Complaint> complaintsMembersWaits;

    public List<Complaint> getComplaintsWaits() {
        complaintsWaits = complaintFacade.getComplaintByStatus(0);
        return complaintsWaits;
    }

    public void setComplaintsWaits(List<Complaint> complaintsWaits) {
        this.complaintsWaits = complaintsWaits;
    }

    public List<Complaint> getComplaintsMembersWaits() {
        complaintsMembersWaits = complaintFacade.getComplaintByStatusAndMember(0, injectedBean.getCustomer());
        return complaintsMembersWaits;
    }

    public void setComplaintsMembersWaits(List<Complaint> complaintsMembersWaits) {
        this.complaintsMembersWaits = complaintsMembersWaits;
    }
    private LinkedHashMap<String, String> technicals = new LinkedHashMap<String, String>();
    private String technicalsSelected;
    List<Complaint> filteredComplaints;
    Complaint complaintSelected;
    String alert = "aa";

    public LinkedHashMap<String, String> getTechnicals() {
        List<Members> temp = membersFacade.getTechnicianAll();
        for (Members Member : temp) {
            technicals.put(Member.getMembersName(), Member.getMembersID().toString() + "," + Member.getMembersName());
        }
        return technicals;
    }

    public void setTechnicals(LinkedHashMap<String, String> technicals) {
        this.technicals = technicals;
    }

    public String getTechnicalsSelected() {
        return technicalsSelected;
    }

    public void setTechnicalsSelected(String technicalsSelected) {
        this.technicalsSelected = technicalsSelected;
    }

    public Login getInjectedBean() {
        return injectedBean;
    }

    public void setInjectedBean(Login injectedBean) {
        this.injectedBean = injectedBean;
    }

    public String getSolution() {
        return solution;
    }

    public String getAlert() {
        return alert;
    }

    public void setAlert(String alert) {
        this.alert = alert;
    }

    public void setSolution(String solution) {
        this.solution = solution;
    }
    String solution;

    public Complaint getComplaintSelected() {
        return complaintSelected;
    }

    public void setComplaintSelected(Complaint complaintSelected) {
        this.complaintSelected = complaintSelected;
    }

    public List<Complaint> getFilteredComplaints() {
        return filteredComplaints;
    }

    public void setFilteredComplaints(List<Complaint> filteredComplaints) {
        this.filteredComplaints = filteredComplaints;
    }

    public List<Complaint> getComplaints() {
        int a = injectedBean.getCustomer().getMembersRole();
        if (injectedBean.getCustomer().getMembersRole() == 1) {
            complaints = complaintFacade.getComplaintByTechnical(injectedBean.getCustomer());
        } else {
            complaints = complaintFacade.getComplaintAll();
        }
        return complaints;
    }

    public void setComplaints(List<Complaint> complaints) {
        this.complaints = complaints;
    }

    public ComplaintsBean() {
    }

    public void deleteComplaint(Complaint complaint) {
        complaintFacade.deleteComplaints(complaint);

    }

    public void createSolution() {
        alert = "Submit Solution Successfull";
        Solution temp = new Solution();
        temp.setComplaintID(complaintSelected);
        temp.setMembersID(injectedBean.getCustomer());
        temp.setSolutionContent(solution);
        temp.setSolutionCreateDate(new Date());
        solutionFacade.createSolution(temp);
        complaintSelected.setComplaintStatus(1);
        complaintSelected.setComplaintCloseDate(new Date());
        complaintFacade.edit(complaintSelected);


    }

    public void updateTechnical() {
        String temp[] = technicalsSelected.split(",");
        Integer id = Integer.valueOf(temp[0]);
        Members member = membersFacade.membersByID(id);
        complaintSelected.setComplaintTechnical(member);
        complaintFacade.edit(complaintSelected);
        alert = "Successful!" + " Assign Task for " + temp[1];

    }

    public void closeDialog() {
        alert = "aa";
    }
}
